package io.xiaojl.xj2ee.modules.sys.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import io.xiaojl.xj2ee.common.exception.XjeeException;
import io.xiaojl.xj2ee.common.kit.PageWrapper;
import io.xiaojl.xj2ee.kit.Query;
import io.xiaojl.xj2ee.modules.sys.entity.SysUserEntity;
import io.xiaojl.xj2ee.modules.sys.mapper.SysUserMapper;
import io.xiaojl.xj2ee.modules.sys.service.SysUserService;


@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserEntity> implements SysUserService {

    @Override
    public PageWrapper<SysUserEntity> queryPage(Map<String, Object> params) {
        Page<SysUserEntity> page = this.selectPage(
                new Query<SysUserEntity>(params).getPage(),
                new EntityWrapper<SysUserEntity>()
        );
        PageWrapper<SysUserEntity> pw = new PageWrapper<>(params);
        pw.setList(page.getRecords());
        pw.setTotalCount(page.getTotal());
        pw.setTotalPage(Integer.parseInt(page.getPages()+""));
        return pw;
    }

	@Override
	public List<Long> queryAllMenuId(Long userId) {
		return baseMapper.queryAllMenuId(userId);
	}

	@Override
	public void save(SysUserEntity user) {
		baseMapper.insert(user);
	}

	@Override
	public void update(SysUserEntity user) {
		baseMapper.updateById(user);
	}

	@Override
	public boolean updatePassword(Long userId, String password, String newPassword) {
		SysUserEntity entity = baseMapper.selectById(userId);
		if(entity!=null){
			if(password.equals(entity.getPassword())){
				entity.setPassword(newPassword);
				
				baseMapper.updateById(entity);
				return true;
			}else{
				throw new XjeeException("更新用户密码：用户ID["+userId+"]原密码不正确!");
			}
		}else{
			throw new XjeeException("更新用户密码：用户ID["+userId+"]不存在!");
		}
	}

	@Override
	public List<String> queryAllPerms(Long userId) {
		return baseMapper.queryAllPerms(userId);
	}

	@Override
	public SysUserEntity selectOne(SysUserEntity user) {
		return baseMapper.selectOne(user);
	}

	@Override
	public int deleteBatchIds(Long[] userIds) {
		return baseMapper.deleteBatchIds(Arrays.asList(userIds));
	}

}
